#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
  if (argc < 2) 
  {
    fprintf(stderr, "Usage..\n");
    exit(1);
  }

  int fd;
  void *addr;
  char *p;
  struct stat statbuf;
  int anum = 0;
  if ((fd = open(argv[1], O_RDONLY)) < 0) 
  {
    perror("open");
    exit(1);
  }

  if (fstat(fd, &statbuf) < 0) 
  {
    perror("fstat");
    exit(1);
  }

  addr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED) 
  {
    perror("mmap()");
    exit(1);
  }

  p = (char *)addr;
  for (int i = 0; i < statbuf.st_size; i++) 
  {
    if (*(p + i) == 'a')
    {
      anum++;    
    }
  }
  munmap(addr, statbuf.st_size);

  printf("the number of a in the file is: %d\n", anum);
  exit(0);
}
